package com.sjm.suchbear.minibookmall.fragment;


import android.app.Fragment;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjm.suchbear.minibookmall.BookDB;
import com.sjm.suchbear.minibookmall.NoticeDetailActivity;
import com.sjm.suchbear.minibookmall.R;
import com.sjm.suchbear.minibookmall.View.MyListView;
import com.sjm.suchbear.minibookmall.action.NoticeManage;
import com.sjm.suchbear.minibookmall.fragment.adapter.NoticeAdapter;
import com.sjm.suchbear.minibookmall.model.NoticeModel;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class NoticeFragment extends Fragment {


    @BindView(R.id.ll_header_back)
    LinearLayout llHeaderBack;
    @BindView(R.id.tv_list_title)
    TextView tvListTitle;
    @BindView(R.id.iv_header_function_ico)
    ImageView ivHeaderFunctionIco;
    @BindView(R.id.ll_header_function)
    LinearLayout llHeaderFunction;
    @BindView(R.id.ll_List_Main)
    RelativeLayout llListMain;
    @BindView(R.id.lv_noticeList)
    MyListView lvNoticeList;
    Unbinder unbinder;

    private List<NoticeModel> notices;
    private SQLiteDatabase dbread;
    private NoticeAdapter noticeAdapter;

    public NoticeFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_notice, container, false);
        unbinder = ButterKnife.bind(this, view);
        BookDB db = new BookDB(getActivity());
        dbread = db.getReadableDatabase();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        llHeaderBack.setVisibility(View.GONE);
        tvListTitle.setText("公告");
        notices = NoticeManage.getNoticeList(dbread);
        noticeAdapter = new NoticeAdapter(getActivity(), notices);
        lvNoticeList.setAdapter(noticeAdapter);
        noticeAdapter.notifyDataSetChanged();
        lvNoticeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //跳转公告详情
                Intent intent = new Intent(getActivity(), NoticeDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("noticemodel", notices.get(i));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
